package tuiLayer;
import java.util.Scanner;

import controlLayer.*;
import modelLayer.*;
import tuiLayer.*;

public class ItemUI {
	
	ItemController itemCtrl = new ItemController();
	String errorMessage = "Incorrect command!";
	ItemContainer itemCont = ItemContainer.getInstance();
	Scanner keyboard = new Scanner(System.in);
	
	public ItemUI(){
		
		writeItemMenu();
		
	}
	
	

	public void writeItemMenu(){
		
		
		System.out.println("");
		System.out.println("");
		System.out.println("***Start item Menu***");
        System.out.println(" (1) Add Item");
        System.out.println(" (2) Find Item");
        System.out.println(" (3) Update Item");
        System.out.println(" (4) Delete Item");
        System.out.println(" (5) Return to main menu");
        
        String choice = getString();
        
        switch(choice){
        case "1" :
        	addItem();
        	writeItemMenu();
        	break;
        
        case "2" :
        	findItem();
        	writeItemMenu();
        	break;	
        	
        case "3" :
        	updateItem();
        	writeItemMenu();
        	break;
        
        case "4" :
        	deleteItem();
        	writeItemMenu();
        	break;
        	
 /*       case "5" :
        	addSet();
        	writeItemMenu();
        	break;
        
        case "6" :
        	findSet();
        	writeItemMenu();
        	break;	
        	
        case "3" :
        	updateSet();
        	writeItemMenu();
        	break;
        
        case "4" :
        	deleteSet();
        	writeItemMenu();
        	break;	
        */
        
        default:      
            System.out.println(errorMessage);
            pressEnter();
            writeItemMenu();
        }
        }
	
	

public void addItem(){
	String name; 
	double price; 
	int barcode; 
	int quantity;
	String location;
	boolean unic;
	
	
	 do{
	        unic=true;    
	        Item i=null;
	        System.out.println("Type barcode: ");
	        barcode = Integer.parseInt(getString());
	        i=itemCtrl.findItem(barcode);
	        if(i!=null){
	        unic=false;
	        System.out.println("Barcode already in the system.\nTry again");
	        }
	}while(!unic);
	 
	 System.out.println("Type the name of Item : ");
	 name = getString();
	 System.out.println("Type the price of the Item");
	 price = Double.parseDouble(getString());
	 System.out.println("Type the quantity of the Item");
	 quantity =  Integer.parseInt(getString());
	 System.out.println("Type the location of the Item");
	 location= getString();
	 
	 Item item = new NormalItem(name,price,barcode,quantity,location);
		itemCont.addNormalItem(item);
		
		System.out.println("Product was added to the database");
	}

public void findItem(){
	int barcode;
	Item i;
	System.out.println("Type barcode to find Item");
	barcode = Integer.parseInt(getString());
	i = itemCtrl.findItem(barcode);
	if(i==null){
		System.out.println("Item is not found");
	}else{
		System.out.println("The name of Item :" + i.getName());
		System.out.println("The price of Item :" + i.getPrice());
		System.out.println("The quantity of Item :" + i.getQuantity());
	}
	}

public void updateItem(){
   
	String name;
	double price;
	int quantity,barcode;
	Item i=null;
	boolean updatedSubMenu=true;
	
	
	
	
	
	
		
    System.out.println("Type barcode of Item you want update");
	barcode = Integer.parseInt(getString());
	i = itemCtrl.findItem(barcode);
	
	if(i== null){
		System.out.println("The Item not found");
		
	}else{
		
		System.out.println("Information about Item :");
		System.out.println("name :" + i.getName());
		System.out.println("price :" + i.getPrice());
		System.out.println("quantity :" + i.getQuantity());
		System.out.println("barcode:" + i.getBarcode());
		System.out.println("**************");
		
	
		while(updatedSubMenu!=true);
	{
		
		System.out.println("");
		System.out.println("**Update Menu**");
		System.out.println("(1) Update name");
		System.out.println("(2) Update price");
		System.out.println("(3) Update barcode");
		System.out.println("(4) Update quantity");
		System.out.println("(5) Cancel and go back to Item Menu");
		
		int choice= Integer.parseInt(getString());
		switch(choice){
		
		case(1):
			System.out.println("Type name you prefered :");
		    name = getString();
		    i.setName(name);
		    System.out.println("System changed the name");
		    
		    break;
		 
		case(2):
			System.out.println("Type price you want to change :");
		    price = Double.parseDouble(getString());
		    i.setPrice(price);
		    System.out.println("System changed the price");
		    break;
		   
		case(3):
			System.out.println("Type new barcode");
		    barcode = Integer.parseInt(getString());
		    i.setBarcode(barcode);
		    System.out.println("System changed the barcode");
		    break;
		case(4):
			System.out.println("Type quantity of Item");
		    quantity = Integer.parseInt(getString());
		    i.setQuantity(quantity);
		    System.out.println("System changed the quantity");
		    break;
		    
		case(5):
			writeItemMenu();
		
		   
		break;
		default:
			System.out.println("Wrong command try again");
			pressEnter();
			
		}
			
		
		
		
		}
		
	}
	}


public void deleteItem(){
	
    Item i =null;
	int barcode;
	System.out.println("Type barcode of Item you want delete");
	barcode = Integer.parseInt(getString());
	 i=itemCtrl.findItem(barcode);
	
	
	
	if(i==null){
		System.out.println("System not found Item");
	}else{
	    if(i instanceof NormalItem){
	    	System.out.println("Item : " + i.getName() + " was deleted");
	    	itemCont.getInstance().removeNormalItem(i);
		}
		
	}
	
	
	
}
	
	
	
	
	
	
	
	
	
	
    private void pressEnter(){
           Scanner scanner=new Scanner(System.in);
           System.out.println("Press enter to continue.");
           scanner.nextLine(); 
}
	private String getString(){
	        String startChoice ;
	        startChoice = keyboard.next();
	        String choice = startChoice.trim();
	        return choice;
}
	}
